1483B - Playlist - CodeForces Solution


data structures dsu implementation *1900

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>

using namespace std;

const int N = (1 << 17);
const int INF = 1e9 + 13;

int gcd(int a, int b) {
    if(!b) return a;
    return gcd(b, a % b);
}
int t[2 * N], p[2 * N];
void update(int pos, int val, int l = 0, int r = N - 1, int v = 1) {
    if(l == r) {
        t[v] = val; 
        p[v] = pos;
        return;
    }
    int m = (l + r) / 2;
    if(pos <= m) update(pos, val, l, m, 2 * v);
    else update(pos, val, m + 1, r, 2 * v + 1);
    t[v] = min(t[2 * v], t[2 * v + 1]);
    p[v] = (t[2 * v] <= t[2 * v + 1] ? p[2 * v] : p[2 * v + 1]);
}
pair<int, int> get(int ql, int qr, int l = 0, int r = N - 1, int v = 1) {
    if(r < ql || l > qr || ql > qr) return {INF, N};
    if(ql <= l && r <= qr) {
        return {t[v], p[v]};
    }
    int m = (l + r) / 2;
    pair<int, int> t1 = get(ql, qr, l, m, 2 * v);
    pair<int, int> t2 = get(ql, qr, m + 1, r, 2 * v + 1);
    return {min(t1.first, t2.first), (t1.first <= t2.first ? t1.second : t2.second)};
}   
set<int> elems;
set<int>::iterator prev(int x) {
    if(elems.empty()) return elems.end();
    return (*elems.begin() < x ? --elems.lower_bound(x) : --elems.end());
}
set<int>::iterator next(int x) {
    if(elems.empty()) return elems.end();
    return (*(--elems.end()) > x ? elems.upper_bound(x) : elems.begin());
}
void solve() {
    vector<int> ans;

    int n;
    cin >> n;
    int a[n];
    for(int i = 0; i < n; i++) 
        cin >> a[i];
    for(int i = 0; i < n; i++) {
        elems.insert(i);
        update(i, gcd(a[i], a[(i + n - 1) % n]));
    }
    int lb = 0;
    while(!elems.empty()) {
        pair<int, int> vi;
        vi = get(*next(lb), n - 1);
        if(vi.first != 1) {
            vi = get(0, n - 1);
            if(vi.first != 1) 
                break;
        }
        update(vi.second, INF);
        elems.erase(vi.second);
        int nx = *next(vi.second), pr = *prev(vi.second);
        ans.push_back(vi.second);
        update(nx, gcd(a[nx], a[pr]));
        lb = nx;
    }
    cout << (int)ans.size() << ' ';
    for(int c : ans) cout << c+1 << ' ';
    elems.clear();
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int t;
    cin >> t;
    while(t--) {
        solve();
        cout << '\n';
    }
}


Comments

Submit
0 Comments
More Questions

46A - Ball Game
114A - Cifera
776A - A Serial Killer
25B - Phone numbers
1633C - Kill the Monster
1611A - Make Even
1030B - Vasya and Cornfield
1631A - Min Max Swap
1296B - Food Buying
133A - HQ9+
1650D - Twist the Permutation
1209A - Paint the Numbers
1234A - Equalize Prices Again
1613A - Long Comparison
1624B - Make AP
660B - Seating On Bus
405A - Gravity Flip
499B - Lecture
709A - Juicer
1358C - Celex Update
1466B - Last minute enhancements
450B - Jzzhu and Sequences
1582C - Grandma Capa Knits a Scarf
492A - Vanya and Cubes
217A - Ice Skating
270A - Fancy Fence
181A - Series of Crimes
1638A - Reverse
1654C - Alice and the Cake
369A - Valera and Plates